package cn.codingjc.aurora.framework.oauth.core.justauth;

import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.utils.UrlBuilder;

/**
 * @ClassName AbstractCustomerRequest
 * @Description 自定义Request
 * @Author jccoding
 * @Date 2024/2/25 10:18
 * @Version 1.0
 **/
public abstract class AbstractCustomerRequest extends AuthDefaultRequest {
    public AbstractCustomerRequest(AuthConfig config, AuthSource source) {
        super(config, source);
    }

    public AbstractCustomerRequest(AuthConfig config, AuthSource source, AuthStateCache authStateCache) {
        super(config, source, authStateCache);
    }

    /**
     * 获取token，由子类实现
     *
     * @param authCallback
     * @return
     */
    @Override
    protected abstract AuthToken getAccessToken(AuthCallback authCallback);

    /**
     * 获取UserInfo，由子类实现
     *
     * @param authToken
     * @return
     */
    @Override
    protected abstract AuthUser getUserInfo(AuthToken authToken);


    /**
     * 返回带{@code state}参数的授权url，授权回调时会带上这个{@code state}
     *
     * @param state state 验证授权流程的参数，可以防止csrf
     * @return 返回授权地址
     * @since 1.11.0
     */
    @Override
    public String authorize(String state) {
        return UrlBuilder.fromBaseUrl(super.authorize(state))
                .queryParam("scope", "read_user+openid")
                .build();
    }
}